	//
	//  GaoInterfaceManager.m
	//
	//  Created by Gikki Ares on 2020/12/31.
	//


#import "JsRequestManager.h"
#import "JsRequestExecutorInterface.h"
#import "JsRequestBuilder.h"
//static __weak id eventTarget = nil;
//static SEL eventAction = nil;
//static IMP eventFun = nil;
//- (void) setEventTarget:(id)target action:(SEL)action {
//	eventTarget = target;
//	eventAction = action;
//	eventFun = target?[target methodForSelector:action]:nil;
//
//}
//
//- (void) sendEventWithId:(int)eventID name:(NSString*)eventName data:(NSString*)eventData {
//	if(eventFun && eventTarget){
//		((void(*)(id,SEL,int,NSString*,NSString*))eventFun)(eventTarget,eventAction,eventID,eventName,eventData);
//	}
//}

static float _defualtTimeout = 10;
@interface JsRequestManager() {
	
	id<JsRequestExecutorInterface> mExecutor;
}

@property (nonatomic,strong) NSMutableDictionary  <NSString *,NSString *>* commonRequestHeader;

@end

@implementation JsRequestManager



- (void)initWithServerAddress:(NSString *)serverAddress {
	_serverAddress = serverAddress;
}

+ (JsRequestManager *)sharedInstance {
	static JsRequestManager * _sharedInstance;
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
		_sharedInstance = [JsRequestManager new];
		_sharedInstance->mExecutor = [JsRequestBuilder sharedInstance];
	});
	return _sharedInstance;
}

- (instancetype)init {
	if(self = [super init]) {
			//默认打开.
		_isGenerateDetailString =  true;
		_commonRequestHeader = [NSMutableDictionary dictionary];
	}
	return self;
}

-(float)timeout {
	if(_timeout == 0) {
		return _defualtTimeout;
	}
	else {
		return _timeout;
	}
}

- (BOOL)isGeneralLogDetail {
	return YES;
}

#pragma mark 2 Interface
- (void)setRequestBuilderImplemention:(id<JsRequestExecutorInterface>)imp {
	mExecutor = imp;
}

-(NSArray *)currentCookieArray {
	return [mExecutor currentCookieArray];
}

- (NSURLSessionDataTask *)invokeDataTaskWithConfig:(JsRequestConfig *)config completionHandler:(JsResponseHandler)handler {
	if(!config.isGenerateDetailString) {
		config.isGenerateDetailString = @(self.isGenerateDetailString);
	}
	if(!config.isCalculateTraffic) {
		config.isCalculateTraffic = @(self.isCalculateTraffic);
	}
	if(!config.serverAddress) {
		config.serverAddress = self.serverAddress;
	}
	if(!config.timeout) {
		config.timeout = @(self.timeout);
	}
	if(_commonRequestHeader.allKeys.count) {
		if(config.requestHeader.allKeys.count) {
			NSMutableDictionary * mdic = [config.requestHeader mutableCopy];
			[mdic addEntriesFromDictionary:_commonRequestHeader];
			config.requestHeader = mdic;
		}
		else {
			//Fix: 这里需要使用mutableCopy,否则不可变.
			config.requestHeader = [_commonRequestHeader mutableCopy];
		}
	}
	
	//配置info
		JsRequestInfo * info = [JsRequestInfo new];
		info.urlString = config.requestUrlString;
		info.requestHeader = config.requestHeader;
		info.requestBodyInputType = config.requestBodyInputType;
	
	return [mExecutor invokeDataTaskWithConfig:config info:info completionHandler:handler];
}


-(void)setValue:(NSString *)value forRequestHeaderKey:(NSString *)key {
	_commonRequestHeader[key] = value;
}

- (void)deleteRequestHeaderKey:(NSString *)key {
	[_commonRequestHeader removeObjectForKey:key];
}


@end
